package com.antifake.impl;

import java.util.Date;
import java.util.Set;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import com.antifake.CodeGenerator;
import com.antifake.CodeGeneratorFactory;

public class StringCodeGeneratorTestCase {
	
	private StringCodeGenerator generator;
	
	@Before
	public void beforeTest() {
		generator = (StringCodeGenerator) CodeGeneratorFactory.createByType(String.class);
	}
	

	@Test
	public void testGenerate1() {
		Set codeSet = generator.generate();
		Assert.assertEquals(codeSet.size(), generator.getCount());
		for(Object obj : codeSet) {
			int digit = generator.getPattern().replace(CodeGenerator.PLACE_HOLDER, "").length() + generator.getDigit();
			Assert.assertEquals(obj.toString().length(), digit);
			System.out.println(obj);
		}
	}
	
	@Test 
	public void testGenerate2() {
		int count = 100;
		int digit = 20;
		String pattern = new Date() + CodeGenerator.PLACE_HOLDER;
		generator.setCount(count);
		generator.setDigit(digit);
		generator.setPattern(pattern);
		Set codeSet = generator.generate();
		Assert.assertEquals(count, codeSet.size());
		for(Object obj : codeSet) {
			int expectedDigit = pattern.replace(CodeGenerator.PLACE_HOLDER, "").length() + 20;
			Assert.assertEquals(expectedDigit, obj.toString().length());
			System.out.println(obj);
		}
	}
}
